package main

type ListNode struct {
	val  int
	next *ListNode
}

func addTwoNumber1(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil || l2 == nil {
		return nil
	}
	head := &ListNode{val: 0, next: nil}
	current := head
	carry := 0
	for l1 != nil || l2 != nil {
		var x, y int
		if l1 == nil {
			x = 0
		} else {
			x = l1.val
		}
		if l2 == nil {
			y = 0
		} else {
			y = l2.val
		}
		current.next = &ListNode{val: (x + y + carry) % 10, next: nil}
		current = current.next
		carry = (x + y + carry) / 10

		if l1 != nil {
			l1 = l1.next
		}
		if l2 != nil {
			l2 = l2.next
		}
	}
	if carry > 0 {
		current.next = &ListNode{val: carry % 10, next: nil}
	}
	return head.next
}
